package verify;

import com.appleyk.core.listener.ACustomVerifyListener;
import com.appleyk.core.model.LicenseExtraParam;
import com.appleyk.core.model.LicenseResult;
import com.appleyk.core.model.LicenseVerifyManager;
import com.appleyk.core.service.AServerInfos;
import de.schlichtherle.license.LicenseContent;
import verify.config.LicenseVerifyProperties;
import verify.listener.LicenseVerifyInstallListener;

import java.io.File;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * @Author: WangYao
 * @description:
 * @date: 2024-05-08 11:16
 */
public class VerifyMain {
    public static LicenseVerifyProperties licenseVerifyProperties = new LicenseVerifyProperties();

    public static void installListener() {

        licenseVerifyProperties.setSubject("landi");
        licenseVerifyProperties.setPublicAlias("publiccert");
        licenseVerifyProperties.setPublicKeysStorePath("/publicCerts.store");
        licenseVerifyProperties.setStorePass("123456a");
        // licenseVerifyProperties.setLicensePath("classpath:license.lic");
        String serverTempPath = AServerInfos.getServerTempPath() + "/lic-files/";
        String latestLicFilePath = getLatestLicFilePath(serverTempPath);
        licenseVerifyProperties.setLicensePath(latestLicFilePath + File.separator + "license.lic");

        // 安装证书
        LicenseVerifyInstallListener.LicenseVerifyInstall(licenseVerifyProperties);
        // 添加自定义校验模式
        initCustomVerifyListener();
        // 校验证书
        LicenseVerify();
    }

    /**
     * 自定义校验模式比如license分发
     */
    public static void initCustomVerifyListener() {
        // new CustomVerifyListenerA();
        // new CustomVerifyListenerB();
    }

    public static void LicenseVerify() {
        LicenseVerifyManager licenseVerifyManager = new LicenseVerifyManager();
        LicenseResult verifyResult = licenseVerifyManager.verify(licenseVerifyProperties.getVerifyParam());
        if (!verifyResult.getResult()) {
            System.out.println(verifyResult.getMessage());
        }
        LicenseContent content = verifyResult.getContent();
        LicenseExtraParam licenseCheck = (LicenseExtraParam) content.getExtra();
        if (verifyResult.getResult()) {
            /** 增加业务系统监听，是否自定义验证 */
            List<ACustomVerifyListener> customListenerList = ACustomVerifyListener.getCustomListenerList();
            boolean compare = true;
            for (ACustomVerifyListener listener : customListenerList) {
                boolean verify = listener.verify(licenseCheck);
                compare = compare && verify;
            }
            if (true){
                System.out.println("licnese 校验成功");
                return;
            }else {
                throw new RuntimeException("license 校验失败");
            }

        }
        throw new RuntimeException(verifyResult.getException().getMessage());
    }

    public static String getLatestLicFilePath(String folderPath) {
        File folder = new File(folderPath);

        if (folder.isDirectory()) {
            File[] files = folder.listFiles();

            if (files != null && files.length > 0) {
                // 按文件最后修改时间排序
                Arrays.sort(files, Comparator.comparingLong(File::lastModified).reversed());

                // 返回最新文件的路径
                return files[0].getAbsolutePath();
            }
        }

        return null;  // 如果文件夹中没有lic文件，返回null或者你认为合适的值
    }
    public static void main(String[] args) {

        VerifyMain.installListener();
        VerifyMain.LicenseVerify();
        VerifyMain.LicenseVerify();
        VerifyMain.LicenseVerify();
    }
}
